package dhu.cst.jnlwt.jiangnan181310209liwanting181310302.tally.fragment_bill;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CalendarView;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;

import dhu.cst.jnlwt.jiangnan181310209liwanting181310302.tally.R;
import dhu.cst.jnlwt.jiangnan181310209liwanting181310302.tally.activity_tally.HomeActivity;
import dhu.cst.jnlwt.jiangnan181310209liwanting181310302.tally.adapter.DetailAdapter;
import dhu.cst.jnlwt.jiangnan181310209liwanting181310302.tally.database.AccountDao;
import dhu.cst.jnlwt.jiangnan181310209liwanting181310302.tally.entity.AccountBean;


public class DetailFragment extends Fragment {

    private String phoneNumber;
    private CalendarView calendarView;
    private ListView listView_detail_expenditure;
    private ListView listView_detail_income;
    private String date;
    DetailAdapter detailAdapter_expenditure;
    DetailAdapter detailAdapter_income;
    List<AccountBean> incomeList;
    List<AccountBean> expenditureList;
    AccountDao dbManager;
    TextView textView_incomeAndDate;
    TextView textView_expenditureAndDate;
    ImageButton back;

    public DetailFragment() {

    }

    public DetailFragment(String phoneNumber) {
        this.phoneNumber=phoneNumber;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_detail, container, false);
        calendarView = view.findViewById(R.id.calendarView);
        listView_detail_expenditure = view.findViewById(R.id.listView_detail_expenditure);
        listView_detail_income = view.findViewById(R.id.listView_detail_income);
        textView_incomeAndDate = view.findViewById(R.id.textView_incomeAndDate);
        textView_expenditureAndDate = view.findViewById(R.id.textView_expenditureAndDate);
        back = view.findViewById(R.id.detail_back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().finish();
            }
        });

        calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
            @Override
            public void onSelectedDayChange(@NonNull CalendarView view, int year, int month, int dayOfMonth) {
                //获取日历点击选择的日期
                Calendar c = Calendar.getInstance();
                c.set(year,month,dayOfMonth);
                date = new SimpleDateFormat("yyyy.MM.dd", Locale.CHINA).format(c.getTime());
                //设置title
                textView_expenditureAndDate.setText(date+"的支出:");
                textView_incomeAndDate.setText(date+"的收入:");
                //根据日期从数据库重新获取数组
                incomeList.clear();
                expenditureList.clear();
                incomeList.addAll(dbManager.getIncomeList(date));
                expenditureList.addAll(dbManager.getExpenditureList(date));
                //更新
                detailAdapter_expenditure.notifyDataSetChanged();
                detailAdapter_income.notifyDataSetChanged();
            }
        });

        dbManager = new AccountDao(this.getActivity(),phoneNumber);
        Calendar calendar = Calendar.getInstance();
        date = new SimpleDateFormat("yyyy.MM.dd", Locale.CHINA).format(calendar.getTime());
        textView_expenditureAndDate.setText(date+"的支出:");
        textView_incomeAndDate.setText(date+"的收入:");
        incomeList = dbManager.getIncomeList(date);
        expenditureList = dbManager.getExpenditureList(date);

        detailAdapter_expenditure = new DetailAdapter(expenditureList,this.getActivity());
        detailAdapter_income = new DetailAdapter(incomeList,this.getActivity());
        listView_detail_expenditure.setAdapter(detailAdapter_expenditure);
        listView_detail_income.setAdapter(detailAdapter_income);
        return view;
    }
}